Page({
  data: {
    realName: '',
    idNumber: '',
    phone: '',
    email: '',
    agreed: false
  },
  
  onLoad() {
    // 从个人信息中获取已填写的信息
    const userInfo = wx.getStorageSync('userInfo') || {}
    
    this.setData({
      realName: userInfo.realName || '',
      idNumber: userInfo.idNumber || '',
      phone: userInfo.phone || '',
      email: userInfo.email || ''
    })
  },
  
  onRealNameInput(e) {
    this.setData({
      realName: e.detail.value
    })
  },
  
  onIdNumberInput(e) {
    this.setData({
      idNumber: e.detail.value
    })
  },
  
  onPhoneInput(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  
  onEmailInput(e) {
    this.setData({
      email: e.detail.value
    })
  },
  
  toggleAgreement() {
    this.setData({
      agreed: !this.data.agreed
    })
  },
  
  // 验证身份证号格式
  validateIdNumber(idNumber) {
    const reg = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/
    return reg.test(idNumber)
  },
  
  // 验证手机号格式
  validatePhone(phone) {
    const reg = /^1[3-9]\d{9}$/
    return reg.test(phone)
  },
  
  // 验证邮箱格式
  validateEmail(email) {
    const reg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
    return reg.test(email)
  },
  
  register() {
    const { realName, idNumber, phone, email, agreed } = this.data
    
    if (!realName || !realName.trim()) {
      wx.showToast({
        title: '请输入真实姓名',
        icon: 'none'
      })
      return
    }
    
    if (!idNumber || !idNumber.trim()) {
      wx.showToast({
        title: '请输入身份证号',
        icon: 'none'
      })
      return
    }
    
    if (!this.validateIdNumber(idNumber)) {
      wx.showToast({
        title: '身份证号格式不正确',
        icon: 'none'
      })
      return
    }
    
    if (!phone || !phone.trim()) {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      })
      return
    }
    
    if (!this.validatePhone(phone)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none'
      })
      return
    }
    
    if (!email || !email.trim()) {
      wx.showToast({
        title: '请输入邮箱地址',
        icon: 'none'
      })
      return
    }
    
    if (!this.validateEmail(email)) {
      wx.showToast({
        title: '邮箱格式不正确',
        icon: 'none'
      })
      return
    }
    
    if (!agreed) {
      wx.showToast({
        title: '请同意注册协议',
        icon: 'none'
      })
      return
    }
    
    // 保存注册信息到本地存储
    const distributorInfo = {
      realName: realName.trim(),
      idNumber: idNumber.trim(),
      phone: phone.trim(),
      email: email.trim(),
      registered: true,
      registerTime: new Date().toISOString()
    }
    wx.setStorageSync('distributorInfo', distributorInfo)
    
    // 更新用户信息中的相关字段（如果还没有填写）
    const userInfo = wx.getStorageSync('userInfo') || {}
    if (!userInfo.realName) userInfo.realName = realName.trim()
    if (!userInfo.idNumber) userInfo.idNumber = idNumber.trim()
    if (!userInfo.phone) userInfo.phone = phone.trim()
    if (!userInfo.email) userInfo.email = email.trim()
    wx.setStorageSync('userInfo', userInfo)
    
    // 更新全局数据
    const app = getApp()
    if (!app.globalData.userInfo) {
      app.globalData.userInfo = {}
    }
    Object.assign(app.globalData.userInfo, userInfo)
    
    wx.showToast({
      title: '注册成功',
      icon: 'success'
    })
    
    // 注册成功后跳转到我的钱包页面
    setTimeout(() => {
      wx.redirectTo({
        url: '/pages/my-wallet/my-wallet'
      })
    }, 1500)
  }
})

